package com.haiheng.module_main.service

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import com.haiheng.library.log.LogUtils
import com.haiheng.library_base.model.ApplicationViewModel
import com.haiheng.library_base.msg.MsgCode
import com.haiheng.library_base.msg.MsgEvent

/**
 *
 * @Des: 扫描监听
 * @Author: hwz
 * @Date:2023/6/14 0014
 * @Version: 1.0
 */
class ScansReceiver : BroadcastReceiver() {
    override fun onReceive(context: Context, intent: Intent) {
        if (intent.action == "com.safeuem.doublebird.appReadyToInstall") {
//                et.append("com.safeuem.doublebird.appReadyToInstall")
            LogUtils.e("com.safeuem.doublebird.appReadyToInstall")
        } else {
            var barcode = intent.getStringExtra("barcode")
            LogUtils.d("读到信息：${barcode}")
            if (barcode != null){
                val event = MsgEvent()
                event.code = MsgCode.MSG_SCANS_BARCODE
                if (barcode.endsWith("\r\n")){
                    event.msg = barcode.replace("\r\n","")
                    ApplicationViewModel.get().even.postValue(event)
                }else if (barcode.endsWith("\r")){
                    event.msg = barcode.replace("\r","")
                    ApplicationViewModel.get().even.postValue(event)
                }else{
                    event.msg = barcode
                    ApplicationViewModel.get().even.postValue(event)
                }


            }

        }
    }
}